package com.gusti.admincasa.services.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.gusti.admincasa.dao.IPortDao;
import com.gusti.admincasa.model.Port;
import com.gusti.admincasa.services.IParallelPortService;
import com.gusti.admincasa.util.AdminCasaResponse;
import com.gusti.admincasa.util.ConstantsUtil;
import com.gusti.admincasa.util.Debug;

/**
 * 
 * @author gusti
 *
 */
public class ParallelPortService implements IParallelPortService {

	@Autowired
	IPortDao portDao;

	public AdminCasaResponse viewPortStatus() {
		AdminCasaResponse adminCasaResponse = new AdminCasaResponse();

		List<Port> listPorts = portDao.listPorts();

		if( listPorts.isEmpty() ) {
			adminCasaResponse.setErrorMsg( ConstantsUtil.ERROR );
		} else {
			adminCasaResponse.setData( listPorts );
		}

		return adminCasaResponse;
	}

	public AdminCasaResponse resetPortStatus() {
		AdminCasaResponse adminCasaResponse = new AdminCasaResponse();

		List<Port> listPorts = portDao.listPorts();

		if( listPorts.isEmpty() ) {
			adminCasaResponse.setErrorMsg( ConstantsUtil.ERROR );
		} else {
			adminCasaResponse.setData( listPorts );
		}
		
		return adminCasaResponse;
	}

	@Transactional
	public AdminCasaResponse updatePortStatus( int portNumber, boolean activated ) {
		AdminCasaResponse adminCasaResponse = new AdminCasaResponse();
		List<Object> response = new ArrayList<Object>();

		Port port = portDao.findPort( portNumber );

		if( port.isNew() ) {
			adminCasaResponse.setErrorMsg( ConstantsUtil.ERROR );
		} else {
			port.setActivated( activated );
			port = portDao.update( port );

			response.add( port );

			//update pp
			List<Object> partialResult = send();
			for (Object object : partialResult) {
				response.add( object );
			}

			adminCasaResponse.setData( response );
		}

		return adminCasaResponse;
	}

	public List<Object> send() {

		List<Object> result = new ArrayList<Object>();

		int number = portDao.getPortStatus();
		result.add( new Integer( number ) );

		StringBuilder command = new StringBuilder()
			.append( "cmd.exe /c " )
			.append( "c:/admincasa/" )
			.append( "send.bat " )
			.append( Integer.toHexString( number ) );

		result.add( command.toString() );

		result.add( processCommandResponse( executeCommand( command.toString() ) ) );

		return result;
	}

	private Process executeCommand( String command ) {
		//"cmd /c dir"
		Process process = null;
		try {
			process = Runtime.getRuntime().exec( command );
		} catch ( IOException e ) {
			Debug.error( e );
		}
		return process;
	}

	private String processCommandResponse( Process process ) {
		StringBuilder response = new StringBuilder();
		try {
			InputStream inputStream = process.getInputStream();
			BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( inputStream ) );
			String line = bufferedReader.readLine();
			while(line != null){
				response.append( line ).append( "\n" );
				line = bufferedReader.readLine();
			}
			inputStream.close();

		} catch ( IOException e ) {
			Debug.error( e );
		}

		return response.toString();
	}

}
